﻿// FTP_Sever.cpp : 定义应用程序的入口点。

#include "FTP_Server.h"

#define MAX_LOADSTRING 100

// 全局变量:
HINSTANCE hInst;                                // 当前实例
CHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
CHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此处放置代码。

    // 初始化全局字符串
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_FTPSERVER, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_FTPSERVER));

    MSG msg;

    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

//注册窗口类
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FTPSERVER));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_FTPSERVER);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

//保存实例句柄并创建主窗口
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中
   
	//得到系统显示器宽和高
   MainWinSize.x = GetSystemMetrics(SM_CXSCREEN);
   MainWinSize.y = GetSystemMetrics(SM_CYSCREEN);

   //记录主窗口矩形区位置
   MainRect.left = 2*MainWinSize.x / 7;
   MainRect.right = 5 * MainWinSize.x / 7;
   MainRect.top = MainWinSize.y / 5;
   MainRect.bottom = 4 * MainWinSize.y / 5;

   //创建主窗口
   HWND hWnd = MAINHWND = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_VSCROLL,
	   MainRect.left, MainRect.top, MainRect.right- MainRect.left, MainRect.bottom - MainRect.top,
	   nullptr, nullptr, hInstance, nullptr);

   //创建日志输出窗口
   logHwnd = CreateWindowA("LISTBOX", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER,0, 0, 
	   MainClientRect.right - MainClientRect.left, MainClientRect.bottom - MainClientRect.top,
	   MAINHWND, (HMENU)IDC_LOG, hInst, NULL);

   int error = GetLastError();

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);


   //给主窗口发送窗口创建完毕消息
   SendMessage(MAINHWND, WM_CREATED, NULL, NULL);

   return TRUE;
}

// 处理主窗口的消息
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_CREATE:
        {
			

            break;
        }

		case WM_CREATED:
		{
			curPath = "C:\\FTPFiles";
			hFont = MYFONT;
			//文件夹不存在则创建
			CreateDirectory(curPath.c_str(), NULL);

			SendMessage(logHwnd, WM_SETFONT, (WPARAM)hFont, 1);

			ContrlPort *ctlServer = new ContrlPort(logHwnd, curPath);
			break;
		}


        case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析菜单选择:
            switch (wmId)
            {
                case IDM_USER:
                    DialogBox(hInst, MAKEINTRESOURCE(IDD_USER), hWnd, PROC_IDD_USER);
                    
                    break;
                default:
                    return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;

		case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hWnd, &ps);
			// TODO: 在此处添加使用 hdc 的任何绘图代码...


			EndPaint(hWnd, &ps);
		}
		break;

        case WM_SIZE:
        {
            //得到系统显示器宽和高
            MainWinSize.x = GetSystemMetrics(SM_CXSCREEN);
            MainWinSize.y = GetSystemMetrics(SM_CYSCREEN);

            //记录主窗口矩形区位置
            MainRect.left = 2 * MainWinSize.x / 7;
            MainRect.right = 5 * MainWinSize.x / 7;
            MainRect.top = MainWinSize.y / 5;
            MainRect.bottom = 4 * MainWinSize.y / 5;

			GetWindowRect(MAINHWND, &MainClientRect);

			MoveWindow(logHwnd, 0, 0,MainClientRect.right - MainClientRect.left,
				MainClientRect.bottom - MainClientRect.top, TRUE);

            break;
        }

		case WM_DESTROY:
			//释放字体数据结构
			DeleteObject(hFont);

			PostQuitMessage(0);
			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}



INT_PTR CALLBACK PROC_IDD_LOGS(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
   switch (message)
   {
        case WM_INITDIALOG:
            return (INT_PTR)TRUE;

        case WM_COMMAND: 
        {


            break;
        }

        case WM_PAINT:
        {
        
        
            break;
        }

        case WM_CLOSE:
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;

        default:
            break;
   }
    return (INT_PTR)FALSE;
}

INT_PTR CALLBACK PROC_IDD_USER(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
        case WM_INITDIALOG:
            return (INT_PTR)TRUE;

        case WM_COMMAND:
        {
            if (LOWORD(wParam) == IDC_ADDUSERBTN || LOWORD(wParam) == IDC_DELUSERBTN)
            {
                
            }

            break;
        }

        case WM_CLOSE:
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
    }
    return (INT_PTR)FALSE;
}
